package com.boss.salary.dao.impl;

import com.boss.framework.dao.BaseDaoImpl;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.IBaseDateDao;
import com.boss.salary.entity.BaseData;
import com.boss.sys.ca.user.dto.UserDTO;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Repository
public class BaseDateDaoImpl extends BaseDaoImpl implements IBaseDateDao {

    @Override
    public List<Map<String, Object>> queryBaseDataList(Map<String, Object> queryParam) {
        StringBuilder sql = new StringBuilder();
        sql.append(" select code,name,ifnull(person_state,0) state  from ").append(TableName.BASE_DATA).append(" where is_deleted = 0 ")
                .append(" and type = ? and mof_div_code = ?");
        Object[] objects = new Object[] {
                queryParam.get("type"),queryParam.get("mof_div_code")
        };
        return super.getjdbcTemplate().queryForList(sql.toString(), objects);
    }

    @Override
    public void saveBaseData(BaseData baseDate) {
        StringBuilder sql = new StringBuilder();
        sql.append(" replace into ").append(TableName.BASE_DATA)
                .append("(id, code, name, type, mof_div_code, is_deleted,person_state) ")
               .append(" VALUES (?,?,?,?,?,?,?)");
        Object[] objects = new Object[] {
                baseDate.getId(),baseDate.getCode(),baseDate.getName(),baseDate.getType()
                ,baseDate.getMof_div_code(), SystemDictConstant.IS_FALSE,baseDate.getPersonState()
        };
         this.getjdbcTemplate().update(sql.toString(), objects);
    }

    @Override
    public void updateBaseData(BaseData baseDate) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" update ").append(TableName.BASE_DATA)
               .append(" set name=? ,person_state=? ");
        paramList.add(baseDate.getName());
                sql.append(" where id=?");
        paramList.add(baseDate.getPersonState());
        paramList.add(baseDate.getId());
        this.getjdbcTemplate().update(sql.toString(), paramList.toArray());
    }

    @Override
    public List<BaseData> queryBaseDataPage(PaginationDTO paginationDTO, Map<String, Object> queryParam) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select id,code,name,type,mof_div_code,is_deleted,person_state from ").append(TableName.BASE_DATA).append(" where is_deleted = 0 ")
                .append(" and type = ? and mof_div_code = ?");
        paramList.add(queryParam.get("type"));
        paramList.add(queryParam.get("mof_div_code"));
        sql.append(" order by code ");
        String querySql = boxUpSql(sql.toString());
        //请求参数初始化
        Object[] params = this.boxUpParam(paramList.toArray(), paginationDTO);
        return this.getJdbcTemplate().query(querySql.toString(), new BeanPropertyRowMapper(BaseData.class), params);
    }

    @Override
    public int queryBaseDataPageTotil(Map<String, Object> queryParam) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select count(id) from ").append(TableName.BASE_DATA).append(" where is_deleted = 0 ")
                .append(" and type = ? and mof_div_code = ?");
        paramList.add(queryParam.get("type"));
        paramList.add(queryParam.get("mof_div_code"));
        //请求参数初始化
        return this.getJdbcTemplate().queryForObject(sql.toString(), Integer.class,paramList.toArray());
    }

    @Override
    public int selectBaseDataIsExist(BaseData baseDate) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select count(id) from ").append(TableName.BASE_DATA).append(" where is_deleted = 0 ")
                .append(" and code = ?  and type = ?  and mof_div_code = ?");
        paramList.add(baseDate.getCode());
        paramList.add(baseDate.getType());
        paramList.add(baseDate.getMof_div_code());
        //请求参数初始化
        return this.getJdbcTemplate().queryForObject(sql.toString(), Integer.class,paramList.toArray());
    }

    @Override
    public BaseData selectBaseDataByTypeCode(String type, String code, String province) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select id,code,name,type,mof_div_code,is_deleted from ").append(TableName.BASE_DATA).append(" where is_deleted = 0 ")
                .append(" and code = ?  and type = ?  and mof_div_code = ? ");
        paramList.add(code);
        paramList.add(type);
        paramList.add(province);
        try {
            //请求参数初始化
            return this.getJdbcTemplate().queryForObject(sql.toString(), BaseData.class, paramList.toArray());
        }catch (Exception e){
            return new BaseData();
        }
    }

    @Override
    public void deleteSalaryType(BaseData baseData, UserDTO userDTO) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" update ").append(TableName.BASE_DATA)
                .append(" set is_deleted = 1 ");
        sql.append(" where id= ?");
        paramList.add(baseData.getId());
        sql.append(" and mof_div_code= ? ");
        paramList.add(userDTO.getProvince());
        this.getjdbcTemplate().update(sql.toString(), paramList.toArray());
    }

    @Override
    public void personTypeSalary(BaseData baseData, UserDTO userDTO) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" update ").append(TableName.SAL_PERSON_TYPE_SALARY)
                .append(" set is_deleted = 1 ");
        sql.append(" where person_type_id= ?");
        paramList.add(baseData.getId());
        sql.append(" and mof_div_code= ? ");
        paramList.add(userDTO.getProvince());
        this.getjdbcTemplate().update(sql.toString(), paramList.toArray());
    }

    @Override
    public List<BaseData> selectBaseDataList(String type, String province) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select id,code,name,type,mof_div_code,is_deleted from ").append(TableName.BASE_DATA).append(" where is_deleted = 0 ")
                .append(" and type = ? and mof_div_code = ?");
        paramList.add(type);
        paramList.add(province);
        sql.append(" order by code ");
        //请求参数初始化
        return this.getJdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper(BaseData.class), paramList.toArray());
    }


    @Override
    public void deleteBaseData(String id) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" update ").append(TableName.BASE_DATA)
                .append(" set is_deleted = 1 ");
        sql.append(" where id= ?");
        paramList.add(id);
        this.getjdbcTemplate().update(sql.toString(), paramList.toArray());
    }
}
